﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace SilverlightControls.Designer
{
    public class FormItem: ContentControl
    {
        private readonly FrameworkElement _element;
        Grid _mainGrid;
        private static readonly Type _thisType = typeof(FormItem);

        #region Propertyes

        FrameworkElement ParentElement { get { return Parent as FrameworkElement; } }
        public FrameworkElement Element { get { return _element; } }

        private double ParentWidth { get { return (ParentElement != null ? ParentElement.ActualWidth : 0); } }
        private double ParentHeight { get { return (ParentElement != null ? ParentElement.ActualHeight : 0); } }

        public double Left
        {
            get { return Margin.Left; }
            set { Margin = new Thickness(value, Margin.Top, ParentWidth - ElementWidth - value, ParentHeight - ElementHeight - Margin.Top); }
        }

        public double Top
        {
            get { return Margin.Top; }
            set { Margin = new Thickness(Margin.Left, value, ParentWidth - ElementWidth - Margin.Left, ParentHeight - ElementHeight - value); }
        }

        #region DependencyPropertyes

        public static readonly DependencyProperty ElementWidthProperty =
            DependencyProperty.Register("ElementWidth", typeof(double), _thisType, new PropertyMetadata((double)0m));

        public double ElementWidth
        {
            get
            {
                if (Element.ActualWidth != Width)
                    ElementWidth = Element.ActualWidth;
                GetValue(ElementWidthProperty);
                return double.IsNaN(Element.Width) ? 0 : Element.Width;
            }
            set { SetValue(ElementWidthProperty, value); }
        }

        public static readonly DependencyProperty ElementHeightProperty =
            DependencyProperty.Register("ElementHeight", typeof(double), _thisType, new PropertyMetadata((double)0m));

        public double ElementHeight
        {
            get
            {
                if (Element.ActualHeight != Height)
                    ElementHeight = Element.ActualHeight;
                GetValue(ElementHeightProperty);
                return double.IsNaN(Element.Height) ? 0 : Element.Height;
            }
            set { SetValue(ElementHeightProperty, value); }
        }
        #endregion

        #endregion

        public FormItem(FrameworkElement element)
        {
            _element = element;
            DefaultStyleKey = typeof(FormItem);
            Resources.Source = new Uri("/SilverlightControls;component/Designer/Themes/Generic.xaml", UriKind.Relative);
        }

        #region Overrides
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _mainGrid = (Grid)GetTemplateChild("MainGrid");
            _mainGrid.Children.Add(_element);
            var thumb = (Thumb)GetTemplateChild("headerThumb");
            thumb.DragDelta += ThumbDragDelta;
            MouseLeftButtonDown += (s, e) => { if (Click != null) Click(this, EventArgs.Empty); };
        }

        #region Drag And Drop

        private void ThumbDragDelta(object sender, DragDeltaEventArgs e)
        {
            if (Click != null) Click(this, EventArgs.Empty);
            Left = Left + e.HorizontalChange;
            Top = Top + e.VerticalChange;
        }

        #endregion

        #endregion

        public event EventHandler<EventArgs> Click;
    }
}
